package view;

import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Observable;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JList;

import entity.User;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;


public class UserListView extends JFrame implements Observer {

    public static Set<User> userSet = new HashSet<User>();
    private DefaultListModel listModel = new DefaultListModel();

    public UserListView(java.util.List<User> users, MouseListener ml) {
        setSize(800, 200);
        for (User user : users) {
            listModel.addElement("[" + user.getUserId() + "] " + user.getVorName() + " " + user.getNachName() + " (state: " + user.getState() + ")");
            user.addObserver(this);
            userSet.add(user);
        }
        JList jlist = new JList();
        jlist.setModel(listModel);
        jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jlist.addMouseListener(ml);
        add(jlist);
        setVisible(true);
    }

    public void update(Observable o, Object arg) {
    	
        User user = (User) o;
        listModel.setElementAt(
                "[" + user.getUserId() + "] " +
                user.getVorName() + " - " +
                user.getNachName() + 
                " (state: " + user.getState() +
                ")"
                , user.getUserId()
        );
    }
}

